# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import json

# useful for handling different item types with a single interface
from itemadapter import ItemAdapter


class WangyiPipeline:
    def __init__(self):
        self.file = open('wangyi.json','w')

    def process_item(self, item, spider):
        item = dict(item)
        #把字典转换成json格式的字符串
        str_data = json.dumps(item,ensure_ascii=False)+',\n'
        self.file.write(str_data)

        return item

    def __del__(self):
        self.file.close()